package com.test.listener;

import com.test.diy_annotation_aop.TestAnnotation;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 简单使用监听,只需要实现ApplicationListener
 */
@Component
public class SimpleListener implements ApplicationListener<ApplicationEvent> {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        // 所有事件每次事件触发最先触发这里
        // 有事件就触发，
        // 项目启动时也会触发，触发的是spring自带的一些事件响应
        System.out.println("1--事件触发：================" + event.getClass().getName());

        Method[] methods = event.getClass().getDeclaredMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(TestAnnotation.class)){
                TestAnnotation testAnnotation = method.getDeclaredAnnotation(TestAnnotation.class);
                System.out.println(testAnnotation.value());
            }
        }
    }
}